package cn.kgm.makeGodV2;

import cn.kgm.makeGodV2.emotion.PersonalityTrait;
import cn.kgm.makeGodV2.event.EventChoice;
import cn.kgm.makeGodV2.event.EventManager;
import cn.kgm.makeGodV2.event.GameEvent;
import cn.kgm.makeGodV2.outCome.EventOutcome;
import cn.kgm.makeGodV2.outCome.TagAddOutcome;
import cn.kgm.makeGodV2.outCome.TraitChangeOutcome;
import cn.kgm.makeGodV2.personalityTag.PersonalityTag;
import cn.kgm.makeGodV2.personalityTag.PersonalityTagManager;
import cn.kgm.makeGodV2.trigger.EventTrigger;
import cn.kgm.makeGodV2.trigger.LocationTrigger;
import cn.kgm.makeGodV2.trigger.TraitTrigger;

import java.util.*;
import java.util.function.Predicate;

/**
 * @Version 1.8
 * @Author: Kappi
 * @Date: 2025-9-16 13:35
 * @Description: cn.kgm.makeGodV2.WorldInitializerV2
 */
public class WorldInitializerV2 {

    private static final Long defaultNpcCount = 10L;

    public static void initializeNPCs(WorldState worldState) {
        worldState.setAllNpcs(initializeNPCList(worldState.getNpcCount()));
    }

    /**
     * @param npcCount npc 总数
     * @return npc 列表<名称,npc对象>
     */
    private static Map<String, GamePlayer> initializeNPCList(Long npcCount) {
        Map<String, GamePlayer> npcMap = new HashMap<>();
        long createNpcCount = Optional.ofNullable(npcCount).orElse(defaultNpcCount);
        for (long i = 0L; i < createNpcCount; i++) {
            GamePlayer npc = GamePlayer.builder()
                    .currentLocation("天地星域")
                    .build();
            String name = createNpcName(npc);
            createTrait(npc);
            createPersonalityTag(npc);
            npcMap.put(name, npc);
        }
        return npcMap;
    }

    private static void createPersonalityTag(GamePlayer npc) {
        PersonalityTagManager tagManager = PersonalityTagManager.builder().build();
        tagManager.initByTraits(npc.getTraits());
        npc.setPersonalityTagManager(tagManager);
    }

    private static String createNpcName(GamePlayer npc) {
        npc.setName("NPC_" + UUID.randomUUID());
        return npc.getName();
    }

    private static GamePlayer createTrait(GamePlayer npc) {
        PersonalityTrait traits = initRandomTraits();
        npc.setTraits(traits);
        return npc;
    }

    private static PersonalityTrait initRandomTraits() {
        return PersonalityTrait.builder()
                .agreeableness(Double.valueOf((int) (Math.random() * 100) + 1))
                .conscientiousness(Double.valueOf((int) (Math.random() * 100) + 1))
                .extraversion(Double.valueOf((int) (Math.random() * 100) + 1))
                .neuroticism(Double.valueOf((int) (Math.random() * 100) + 1))
                .openness(Double.valueOf((int) (Math.random() * 100) + 1))
                .build();
    }


    public static EventManager initializeEvent(WorldState worldState) {
        // 创建随机事件管理器
        EventManager eventManager = new EventManager(worldState);
        // 创建事件触发方式
        List<EventTrigger> triggers = Arrays.asList(
                // 创建位置触发
                new LocationTrigger("天地星域"),
                // 创建性格特征触发
                new TraitTrigger("openness", 60, 100));

        // 创建事件结束后选项
        List<EventOutcome> observeOutcomes = Arrays.asList(
                new TraitChangeOutcome("openness", 5),
                new TagAddOutcome(PersonalityTag.builder()
                        .name("随机标签")
                        .id("random_tag")
                        .build())
        );

        Predicate<GamePlayer> observeCondition = n ->
                n.getTraits().getOpenness() >= 70 &&
                        n.getTraits().getConscientiousness() >= 60;
        EventChoice observeChoice = new EventChoice(
                "choice_observe",
                "静静观察",
                observeCondition,
                observeOutcomes,
                80.0
        );

        GameEvent valleyEvent = new GameEvent(
                "qiren_valley_encounter",
                "奇人谷遭遇",
                "{npc}在奇人谷中偶然发现了一位正在垂钓的神秘老者。",
                triggers,
                Arrays.asList(observeChoice) // 实际应该有多个选项
        );
        eventManager.addEvent(valleyEvent);

        return eventManager;
    }
}
